我正在开发一款应用,可以访问您设备中的所有图片并自动将图像上传到Firebase存储。
这就像活动开始时一样,它会自动获取图像并上传而无需用户选择。
Main_Activity :
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Cursor cursor;
private int columnIndex;
GridView AllImages;
public int REQUEST_PERMISSIONS = 123;
Uri myUri;
ImageView img;
FirebaseApp firebaseApp;
FirebaseDatabase firebaseDatabase;
FirebaseStorage firebaseStorage;
StorageReference storageReference;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ((ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) && (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
}
} else {
Log.e("Else", "Else");
}
img = (ImageView) findViewById(R.id.images);
ArrayList<String> arrayList = getImagesPath(this);
String a = arrayList.get(6);
Toast.makeText(getApplicationContext(), "" + a.toString(), Toast.LENGTH_LONG).show();
img.setImageURI(Uri.parse(new File(a).toString()));
myUri = Uri.parse(new File(c).toString());
firebaseApp = FirebaseApp.getInstance();
firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
storageReference = firebaseStorage.getReference("Pictures");
StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "." + getFileExtension(myUri));
sRef.putFile(myUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
Toast.makeText(getApplicationContext(), "Can't Upload ", Toast.LENGTH_LONG).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
// Setting progressDialog Title.
Toast.makeText(getApplicationContext(), "Uploading ", Toast.LENGTH_LONG).show();
}
});
String[] projection = {MediaStore.Images.Thumbnails._ID};
cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID + "");
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
AllImages = (GridView) findViewById(R.id.GV_Images);
AllImageAdapter adapter = new AllImageAdapter(MainActivity.this, cursor, columnIndex);
AllImages.setAdapter(adapter);
}
public String getFileExtension(Uri uri) {
ContentResolver cR = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cR.getType(uri));
}
public static ArrayList<String> getImagesPath(Activity activity) {
Uri uri;
ArrayList<String> listOfAllImages = new ArrayList<String>();
Cursor cursor;
int column_index_data, column_index_folder_name;
String PathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = activity.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
while (cursor.moveToNext()) {
PathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(PathOfImage);
}
return listOfAllImages;
}
}
适配器类:
public class AllImageAdapter extends BaseAdapter {
Context c;
Cursor cur;
ImageView imageView;
int columnIndex;
public AllImageAdapter(Context c, Cursor cur, int columnIndex)
{
this.c = c;
this.cur = cur;
this.columnIndex = columnIndex;
}
@Override
public int getCount() {
return cur.getCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Set The Image in Image View.
imageView = new ImageView(c);
cur.moveToPosition(position);
int imageID = cur.getInt(columnIndex);
imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
imageView.setLayoutParams(new GridView.LayoutParams(330, 330));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(4, 4, 4, 4);
//convertView.setTag(imageView);
return imageView;
}
}
在(Main Activity
)暂时,我只是自己在索引6上挑选一张照片,以检查它是否有效。
而且这里
我的另一个班级:
public class Constants {
public static final String STORAGE_PATH_UPLOADS = "uploads/";
//public static final String DATABASE_PATH_UPLOADS = "uploads";
}
我还没有使用数据库。
我遇到的问题是,无论何时程序运行,它都会显示异常:
StorageException
已经发生。
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
09-08 12:06:44.694 11024-11263/com.example.umairasher.picturestofirebase
E/StorageException: No content provider: received_1035650876484715.jpeg
java.io.FileNotFoundException: No content provider: received_1035650876484715.jpeg
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1092)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:944)
at android.content.ContentResolver.openInputStream(ContentResolver.java:664)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at com.example.umairasher.picturestofirebase.MainActivity.onCreate(MainActivity.java:113)
at android.app.Activity.performCreate(Activity.java:6439)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605)
at android.app.ActivityThread.access$1100(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:179)
at android.app.ActivityThread.main(ActivityThread.java:5730)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
E/StorageException: No content provider: received_1035650876484715.jpeg
java.io.FileNotFoundException: No content provider: received_1035650876484715.jpeg
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1092)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:944)
at android.content.ContentResolver.openInputStream(ContentResolver.java:664)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at com.example.umairasher.picturestofirebase.MainActivity.onCreate(MainActivity.java:113)
at android.app.Activity.performCreate(Activity.java:6439)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605)
at android.app.ActivityThread.access$1100(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:179)
at android.app.ActivityThread.main(ActivityThread.java:5730)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
任何人都可以帮忙,我知道我在某个地方犯了一个错误,但却无法帮助它。提前谢谢。