gpt4 book ai didi

android - 无法使用 4.2.2 AVD 上的 Intent 选择器从相机获取图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:46 29 4
gpt4 key购买 nike

我正在开发我的应用程序的一部分,它允许用户使用 Intent 选择器从相机或图库中选择图像。

它在我的 2.2.1 android 手机上工作正常,但是当我在 4.2.2 AVD 上编译它时,当我使用相机时它返回一个空指针错误,

 public void onClick(View View) 
{
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);


Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "Chooser");

Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser,REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
try
{
if (bitmap != null)
{
bitmap.recycle();
}

InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}

catch (FileNotFoundException e)
{
e.printStackTrace();
}

catch (IOException e)
{
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
}

这是我得到的错误:
05-05 05:03:31.730: E/AndroidRuntime(820): 由 java.lang.NullPointerException 引起

它说问题出在这一行:

InputStream stream = getContentResolver().openInputStream(data.getData());

我做错了什么?

更新:

现在它可以工作了,这里是解决方案:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
if(data.getData()!=null)
{
try
{
if (bitmap != null)
{
bitmap.recycle();
}

InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}

catch (FileNotFoundException e)
{
e.printStackTrace();
}

catch (IOException e)
{
e.printStackTrace();
}
}

else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}

super.onActivityResult(requestCode, resultCode, data);
}
}

感谢您的帮助!

最佳答案

您好,请尝试使用此代码。

以下代码用于相机按钮点击工作:

imgviewCamera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//define the file-name to save photo taken by Camera activity
String fileName = "new-photo-name.jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
}
});


OnActivityresult code will be like below



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
//use imageUri here to access the image
selectedImageUri = imageUri;

} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
break;
}

if(selectedImageUri != null){
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();

// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);

if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}

if (filePath != null) {

Toast.makeText(getApplicationContext(), " path" + filePath,
Toast.LENGTH_LONG).show();

Intent i = new Intent(getApplicationContext(), EditorActivity.class);
// passing array index
i.putExtra("id", filePath);
startActivity(i);

}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}



}

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}

不要忘记在 list 文件中添加相机权限。
希望这会帮助你。

关于android - 无法使用 4.2.2 AVD 上的 Intent 选择器从相机获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16381616/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com