当我在我的照片编辑器 Android 应用程序上工作时,通过应用程序从相机捕获图像后,我正在调整图像大小,然后将图像保存在手机的 Gallery/Photos 文件夹中
以下是 HomeActivity 代码:
package com.saashtechs.photoeditor;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Objects;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "HomeActivity";
private static final int GALLERY_RESULT = 1;
private static final int CAMERA_RESULT = 2;
private static final String FILE_PROVIDER_AUTHORITY = "com.saashtechs.photoeditor";
private static final int CAMERA_PERMISSION_REQ_CODE = 1001;
private static final int STORAGE_PERMISSION_REQ_CODE = 1002;
private Uri imageToUploadUri;
private String mCapturedImagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
public void openCamera(View view) {
// check for camera permission if not granted before
if (ContextCompat.checkSelfPermission(this, CAMERA) != PERMISSION_GRANTED) {
String[] cameraPermission = {CAMERA};
ActivityCompat.requestPermissions(this, cameraPermission, CAMERA_PERMISSION_REQ_CODE);
} else {
dispatchImageCaptureIntent();
}
}
public void openGallery(View view) {
// check for storage permission if not granted before
if (ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) != PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
String[] storagePermissions = {READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this, storagePermissions, STORAGE_PERMISSION_REQ_CODE);
} else {
dispatchGalleryIntent();
}
}
private void dispatchGalleryIntent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_RESULT);
}
private void dispatchImageCaptureIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null) {
Uri photoFileUri = FileProvider.getUriForFile(this, FILE_PROVIDER_AUTHORITY, photoFile);
Log.d(TAG, "dispatchImageCaptureIntent:photoFileUri: " + photoFile.toString());
//Add URI to imageToUploadUri. You forgot to add it.
imageToUploadUri = photoFileUri;
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
startActivityForResult(cameraIntent, CAMERA_RESULT);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_PERMISSION_REQ_CODE:
if (grantResults[0] == PERMISSION_GRANTED) {
dispatchImageCaptureIntent();
} else {
Toast.makeText(this, "Required camera permission not granted", Toast.LENGTH_SHORT).show();
}
break;
case STORAGE_PERMISSION_REQ_CODE:
if (grantResults[0] == PERMISSION_GRANTED) {
dispatchGalleryIntent();
} else {
Toast.makeText(this, "Required storage permission not granted", Toast.LENGTH_SHORT)
.show();
}
break;
default:
throw new IllegalArgumentException("Unexpected request code");
}
}
private File createImageFile() throws IOException {
String timeStamp = DateFormat.getDateTimeInstance().format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
mCapturedImagePath = image.getAbsolutePath();
Log.d(TAG, "createImageFile: " + mCapturedImagePath);
return image;
}
private Bundle uriToBundle(Uri imageUri) {
Bundle bundle = new Bundle();
bundle.putString(MainActivity.IMAGE_URI, imageUri.toString());
return bundle;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == GALLERY_RESULT) {
Uri imageUri = data.getData();
startActivity(MainActivity.getIntent(this, uriToBundle(Objects.requireNonNull(imageUri))));
} else if (requestCode == CAMERA_RESULT) {
File imageFile = new File(mCapturedImagePath);
Bitmap image = BitmapFactory.decodeFile(mCapturedImagePath);
image = Bitmap.createScaledBitmap(image, 300, 300, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try {
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "filename.jpg");
boolean result;
result = file.createNewFile();
if (result) {
FileOutputStream fo = new FileOutputStream(imageFile);
fo.write(bytes.toByteArray());
fo.close();
}
} catch(IOException ie) {
ie.printStackTrace();
}
startActivity(MainActivity.getIntent(this, uriToBundle(Objects.requireNonNull(imageToUploadUri))));
}
} else {
Toast.makeText(this, "Image not loaded.", Toast.LENGTH_SHORT).show();
}
}
public static Intent getIntent(Context context) {
return new Intent(context, HomeActivity.class);
}
}
manifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.saashtechs.photoeditor">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name="com.saashtechs.photoeditor.MainApplication"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:roundIcon="@drawable/app_icon"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.saashtechs.photoeditor.MainActivity">
</activity>
<activity
android:name="com.saashtechs.photoeditor.HomeActivity"
android:screenOrientation="portrait"
android:theme="@style/MyAppTheme">
<meta-data
android:name="com.google.android.gms.ads.com.saashtechs.photoeditor"
android:value="@string/banner_id"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.saashtechs.photoeditor"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
因此,当我捕获图像然后当用户被带到编辑应用程序时,它没有,它在捕获图像后再次将我带回主页
错误日志是:
2018-11-03 19:20:14.831 20452-20452/com.saashtechs.photoeditor E/AdobeImageEditorActivity: using a temporary file!
2018-11-03 19:20:15.565 21173-21248/? E/msgr.BootIdReader: Error reading boot_id from procfs
java.io.FileNotFoundException: /proc/sys/kernel/random/boot_id (Permission denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:200)
at java.io.FileInputStream.<init>(FileInputStream.java:150)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileReader.<init>(FileReader.java:58)
at X.1od.a(:322785)
at X.482.a(:570546)
at X.0JC.A(Unknown Source:26)
at X.0Jl.a(:61297)
at X.0Jl.b(:61321)
at X.3RW.d(:485114)
at X.3RW.b(:485105)
at X.3RW.a(:485067)
at X.3Wz.init(:500062)
at X.0Pq.run(:76753)
at X.0Or.run(:72967)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at X.0Q1.run(:77237)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at X.0MY.run(:67135)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at X.0Q3.run(:77257)
at java.lang.Thread.run(Thread.java:764)
2018-11-03 19:20:15.980 21173-21235/? E/msgr.FDLeakDetector: Can only be constructed after a call to init()
2018-11-03 19:20:16.267 587-587/? E/SELinux: avc: denied { find } for interface=android.hardware.memtrack::IMemtrack pid=21173 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:hal_memtrack_hwservice:s0 tclass=hwservice_manager permissive=0
2018-11-03 19:20:16.268 21173-21273/? E/memtrack: Couldn't load memtrack module
2018-11-03 19:20:16.486 21173-21278/? E/CompactDiskManagerImpl.cpp: Failed to remove new folder structure directories: No such file or directory
2018-11-03 19:20:16.779 21173-21257/? E/SoLoader: couldn't find DSO to load: libreliability.so
2018-11-03 19:20:18.652 748-2390/? E/IzatSvc_ComboNetworkProvider: Exiting with error proc line 208 "1"
2018-11-03 19:20:20.948 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.949 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.958 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.959 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.965 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.966 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.973 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:20.973 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.009 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.010 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.012 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.012 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.014 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.014 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.015 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:21.016 3122-3304/? E/MarketManager: String array resource ID #0x0
2018-11-03 19:20:22.469 18103-18147/? E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=20452, uid=10297 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceWritePermissionInner(ContentProvider.java:713)
at android.content.ContentProvider$Transport.enforceWritePermission(ContentProvider.java:515)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:260)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:152)
at android.os.Binder.execTransact(Binder.java:702)
2018-11-03 19:20:22.486 20452-20452/com.saashtechs.photoeditor E/SaveHiResImageTask: save task cancelled
--------- beginning of crash
2018-11-03 19:20:22.491 20452-21251/com.saashtechs.photoeditor E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.saashtechs.photoeditor, PID: 20452
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=20452, uid=10297 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:2005)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1555)
at com.adobe.creativesdk.aviary.internal.media.MediaUtils.insertImage(MediaUtils.java:36)
at com.adobe.creativesdk.aviary.AdobeImageEditorActivityAbstract$SaveHiResImageTask.doInBackground(AdobeImageEditorActivityAbstract.java:2004)
at com.adobe.creativesdk.aviary.AdobeImageEditorActivityAbstract$SaveHiResImageTask.doInBackground(AdobeImageEditorActivityAbstract.java:1905)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
2018-11-03 19:20:22.643 765-765/? E/lowmemorykiller: Error writing /proc/20452/oom_score_adj; errno=22
2018-11-03 19:20:22.814 2000-2136/? E/InputDispatcher: channel 'b1f7fa com.saashtechs.photoeditor/com.adobe.creativesdk.aviary.AdobeImageEditorActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-03 19:20:22.815 2000-2136/? E/InputDispatcher: channel 'c744e87 com.saashtechs.photoeditor/com.adobe.creativesdk.aviary.AdobeImageEditorActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-03 19:20:22.830 2000-2136/? E/InputDispatcher: channel '1ec9121 com.saashtechs.photoeditor/com.saashtechs.photoeditor.HomeActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-03 19:20:22.879 2000-3121/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.miui.home/com.miui.home.launcher.Launcher/4053012
2018-11-03 19:20:22.880 751-809/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
2018-11-03 19:20:22.928 2406-21299/? E/NetworkScheduler: Invalid component specified.
你能告诉我我哪里错了吗?
提前致谢