讀取手機所有圖片檔

1.UI的xml檔

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnGetImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="47dp"
        android:layout_x="59dp"
        android:layout_y="262dp"
        android:text="Get SysDB Picture" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="76dp"
        android:layout_y="327dp"
        android:text="Next  Picture" />

    <Button
        android:id="@+id/read_buton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="68dp"
        android:layout_x="66dp"
        android:layout_y="390dp"
        android:text="Read Picture Path" />

    <ImageView
        android:id="@+id/myImg"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_x="39dp"
        android:layout_y="34dp"
        android:src="@drawable/ic_launcher" />

</AbsoluteLayout>

2.主程式碼

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
Cursor cursor;
String path;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.btnGetImg);
Button read = (Button) findViewById(R.id.read_buton);
iv = (ImageView) findViewById(R.id.myImg);
bt.setOnClickListener(this);
Button bt2 = (Button) findViewById(R.id.btnNext);
bt2.setOnClickListener(this);
read.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.btnGetImg:
startGetPic();
break;
case R.id.btnNext:
getNext();
break;
case R.id.read_buton:
readPath();
break;
}
}

public void readPath() {

try {
Toast.makeText(this, path, Toast.LENGTH_SHORT).show();
} catch (Throwable io) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}

public void getNext() {
try {
cursor.moveToNext();
if (cursor.isAfterLast())
cursor.moveToFirst();
String filepath = cursor.getString(1);
path = filepath;
Bitmap bm = BitmapFactory.decodeFile(filepath);
iv.setImageBitmap(bm);
} catch (Throwable io) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();

}
}

// MediaStore.Images.Media.DISPLAY_NAME+" like '%2013-07-06%'"
public void startGetPic() {
try {
String[] projection = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.SIZE };
cursor = this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
null, null, null);
cursor.moveToFirst();
String filepath = cursor.getString(1);
path = filepath;
Bitmap bm = BitmapFactory.decodeFile(filepath);
iv.setImageBitmap(bm);
} catch (Throwable io) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}
}

沒有留言: