嘿,我目前正在制作一个动态壁纸,我允许用户选择一个将落后于我的效果的图像.
目前我有:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.putExtra("crop", "true");
startActivityForResult(i, 1);
Run Code Online (Sandbox Code Playgroud)
稍微下:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Log.d("IMAGE SEL", "" + selectedImage);
// TODO Do something with the select image URI
SharedPreferences customSharedPreference = getSharedPreferences("imagePref", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference.edit();
Log.d("HO", "" + selectedImage);
editor.putString("imagePref", getRealPathFromURI(selectedImage));
Log.d("IMAGE SEL", getRealPathFromURI(selectedImage));
editor.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
当我的代码运行时,Logcat告诉我selectedImage为null.如果我评论出来的话
i.putExtra("crop", "true"):
Run Code Online (Sandbox Code Playgroud)
Logcat没有给我空指针异常,我能够用图像做我想做的事情.那么,这里的问题是什么?有没有人知道如何解决这个问题?谢谢你的时间.
我已经编写了自己的ImageViewer,现在我希望将Set设置为 Android原生ImageViewer中的功能.我现在有可能,因为Facebook有它.我附上了截图,让自己更加清晰.
PS我想更详细地解释出现了什么问题.在菜单中选择"联系人图标"后,将显示我的联系人列表.当我选择联系人时,申请人关闭.如果我选择"Home/Lock screen wallpaper",它会打开我手机的图库.这是我的代码片段:
Bitmap icon = mBitmap;
Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
setAs.setType("image/jpg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "/my_tmp_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
setAs.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/my_tmp_file.jpg"));
startActivity(Intent.createChooser(setAs, "Set Image As"));
Run Code Online (Sandbox Code Playgroud)
我还为我的清单添加了后续权限,我可以将我的图像写入手机的SD卡.
