开发人员。我正在做类似instagram故事的事情,但是我很难选择从手机相机拍摄的照片,但照片却坏了。我的活动1是回收视图,其中显示了出版物或故事,第二个活动是他们撰写和选择图片的地方,但是转到第三个活动并看到它很大。
public class PublicarActivity extends AppCompatActivity {
private static final String TAG = "ImagePicker";
private static final int DEFAULT_MIN_WIDTH_QUALITY = 400;
public static int minWidthQuality = DEFAULT_MIN_WIDTH_QUALITY;
EditText mTitleEt, mDescEt;
ImageView mPostIv;
Button mUploadBtn;
String img;
String mStoragePath = "Restaurantes";
String mDatabasePath = "Data";
Uri mFilePathUri;
StorageReference mStorageReference;
DatabaseReference mDatabaseReference;
ProgressDialog mProgressDialog;
int IMAGE_REQUEST_CODE = 5;
private Uri mImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_publicar);
mTitleEt = findViewById(R.id.pTitleEt);
mDescEt = findViewById(R.id.pDescrEt);
mPostIv = findViewById(R.id.pImageIv);
mUploadBtn = findViewById(R.id.pUploadBtn);
mPostIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), IMAGE_REQUEST_CODE);
}
});
mUploadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadDataFirease();
}
});
mStorageReference = FirebaseStorage.getInstance().getReference();
mDatabaseReference = FirebaseDatabase.getInstance().getReference(mDatabasePath);
mProgressDialog = new ProgressDialog(PublicarActivity.this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Clear the Activity's bundle of the subsidiary fragments' bundles.
outState.clear();
}
private void uploadDataFirease() {
if (mFilePathUri != null){
mProgressDialog.setTitle("Cargando Imagen...");
mProgressDialog.show();
StorageReference storageReference2nd = mStorageReference.child(mStoragePath + System.currentTimeMillis() + "." + getFileExtension(mFilePathUri));
storageReference2nd.putFile(mFilePathUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String mPostTitle = mTitleEt.getText().toString().trim();
String mPostDesc = mDescEt.getText().toString().trim();
mProgressDialog.dismiss();
Toast.makeText(PublicarActivity.this, "Cargando Imagen", Toast.LENGTH_SHORT).show();
ImageUploadInfo imageUploadInfo = new ImageUploadInfo(mPostTitle, mPostDesc, taskSnapshot.getDownloadUrl().toString());
String imageUploadId = mDatabaseReference.push().getKey();
mDatabaseReference.child(imageUploadId).setValue(imageUploadInfo);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
mProgressDialog.dismiss();
Toast.makeText(PublicarActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
mProgressDialog.setTitle("Cargando Imagen...");
}
});
}
else {
Toast.makeText(this, "Please", Toast.LENGTH_SHORT).show();
}
}
private String getFileExtension(Uri mFilePathUri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(mFilePathUri));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ContentResolver cr = this.getContentResolver();
Bitmap bit = null;
if (requestCode == IMAGE_REQUEST_CODE
&& resultCode == RESULT_OK
&& data != null
&& data.getData() !=null) {
mImageUri = data.getData();
mFilePathUri = data.getData();
InputStream imageStream = null;
int rotate = 0;
int orientation = 0;
try {
imageStream = getContentResolver().openInputStream(mFilePathUri);
img = getPath(mFilePathUri);
File file = new File(img);
mFilePathUri = Uri.fromFile(file);
ExifInterface exif = new ExifInterface(
file.getAbsolutePath());
orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Log.d("Rotacion original: ", String.valueOf(orientation));
Log.d("Rotacion: ", String.valueOf(rotate));
bit = BitmapFactory.decodeStream(imageStream);
//bit = getImageResized(getApplicationContext(),mFilePathUri);
mPostIv.setImageBitmap(Bitmap.createScaledBitmap(bit, 500, 500, false));
}
}
public String getPath(Uri uri) {
String[] projection ={ MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
AssetFileDescriptor fileDescriptor = null;
try {
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(
fileDescriptor.getFileDescriptor(), null, options);
Log.d(TAG, options.inSampleSize + " sample method bitmap ... " +
actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight());
return actuallyUsableBitmap;
}
private static Bitmap getImageResized(Context context, Uri selectedImage) {
Bitmap bm = null;
int[] sampleSizes = new int[]{5, 3, 2, 1};
int i = 0;
do {
bm = decodeBitmap(context, selectedImage, sampleSizes[i]);
Log.d(TAG, "resizer: new bitmap width = " + bm.getWidth());
i++;
} while (bm.getWidth() < minWidthQuality && i < sampleSizes.length);
return bm;
}
}