我正在尝试从厨房中选择一个图像然后处理它,我是通过按下主活动上的一个按钮来完成的,然后开始一个新的活动,您可以在其中选择内置图像或选择你自己的,当我想选择我自己的图像时,它应该打开画廊,让我选择一个图像,为什么这不起作用(虽然它之前工作,我想我只是打破了一些东西,但不要知道什么)
我的代码:
package il.popov.ecptest;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class select_activity extends ListActivity {
ListView listView;
Intent intent;
@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] StringArray = {
"Select your own", "image1", "image2", "image3", "image4", "None"
};
setListAdapter(new ArrayAdapter < String > (this, R.layout.support_simple_spinner_dropdown_item, StringArray));
intent = new Intent(this.getApplicationContext(), main_activity.class);
listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@
Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position, long arg3) {
processClick(position);
}
});
}
public void processClick(int position) {
switch (position) {
case 0:
//Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
//startActivity(i);
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 237);
//Returns to main activity , DO NOT CHANGE
startActivity(intent);
break;
case 1:
midClass.img = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
midClass.isImageSelected = true;
startActivity(intent);
break;
case 2:
midClass.img = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
midClass.isImageSelected = true;
startActivity(intent);
break;
case 3:
midClass.img = BitmapFactory.decodeResource(getResources(), R.drawable.image3);
midClass.isImageSelected = true;
startActivity(intent);
break;
case 4:
midClass.img = BitmapFactory.decodeResource(getResources(), R.drawable.image4);
midClass.isImageSelected = true;
startActivity(intent);
break;
case 5:
midClass.img = null;
midClass.isImageSelected = false;
startActivity(intent);
break;
default:
midClass.toastificate("Error in selection process");
midClass.isImageSelected = false;
midClass.img = null;
break;
}
}
@
Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
midClass.toastificate("checking result");
if (requestCode == 237 && resultCode == RESULT_OK && null != data) {
midClass.toastificate("Processing image");
Uri selectedImage = data.getData();
String[] filePathColumn = {
MediaStore.Images.Media.DATA
};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
midClass.img = BitmapFactory.decodeFile(picturePath, opt);
if (midClass.img != null) {
midClass.isImageSelected = true;
} else {
midClass.isImageSelected = false;
}
}
}
}
我使用了以下权限:
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
答案 0 :(得分:0)
case 0:
//Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
//startActivity(i);
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 237);
//Returns to main activity , DO NOT CHANGE
startActivity(intent);// Remove this line in your case 0