你好我试图在我的应用程序中保存从url中获取的图片,但当我尝试访问内存以放置数据时,出现错误
无法解码流java.io.FileNotFoundException /storage/emulated/0/Pictures/Messenger/20180117_113943.jpg(权限被拒绝)
我想为我的应用程序选择图片。
这是我的代码:
public class MainActivity extends ActionBarActivity {
protected static final int REQUEST_CAMERA = 111;
protected static final int SELECT_FILE = 222;
private Bitmap m_bitmap1;
Global mGlobal;
AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
//Locate the Banner Ad in activity_main.xml
adView = (AdView) this.findViewById(R.id.ads);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
.build();
// Load ads into Banner Ads
try {
adView.loadAd(adRequest);
} catch (Exception e) {
}
mGlobal = ((Global) getApplication());
findViewById(R.id.camera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
}
});
findViewById(R.id.galery).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// super.onActivityResult(requestCode, resultCode, data);
// /==========================
if (resultCode == RESULT_OK) {
Intent i = new Intent(MainActivity.this, SelectcropActivity.class);
if (requestCode == REQUEST_CAMERA) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
btmapOptions.inSampleSize = 2;
m_bitmap1 = BitmapFactory.decodeFile(f.getAbsolutePath(),
btmapOptions);
mGlobal.setImage(m_bitmap1);
startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri,
MainActivity.this);
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
Bitmap bm1 = BitmapFactory.decodeFile(tempPath, btmapOptions);
Matrix mat = new Matrix();
Bitmap bMapRotate = Bitmap.createBitmap(bm1, 0, 0,
bm1.getWidth(), bm1.getHeight(), mat, true);
mGlobal.setImage(bMapRotate);
startActivity(i);
}
}
}
public String getPath(Uri uri, Activity activity) {
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = activity
.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}}
这是注销:
01-27 19:07:08.609 20624-20624/com.ideanet.animalfacemorph E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Messenger/20180117_113943.jpg (Permission denied)
01-27 19:07:08.611 20624-20624/com.ideanet.animalfacemorph D/AndroidRuntime: Shutting down VM
01-27 19:07:08.612 20624-20624/com.ideanet.animalfacemorph E/UncaughtException: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=222, result=-1, data=Intent { dat=content://media/external/images/media/9528 flg=0x1 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) }} to activity {com.ideanet.animalfacemorph/com.ideanet.animalfacemorph.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
答案 0 :(得分:0)
确保您的AndroidManifest.xml
文件中包含以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>