H.fate H.fate - 1 year ago 72
Android Question

Imageuploding on okhttp3 or Retrofit2, how can i get this uri?

I want to make service for imageuploading using retrofit2 or Okhttp3,

CODE :

public class GreenFragment extends Fragment {

#I omitted onCreateView that is for checking permission and trigger startGallry()

private void startGallery() {
Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
cameraIntent.setType("image/*");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
if(requestCode == 1000){
Uri returnUri = data.getData();
Bitmap bitmapImage = null;
try {
bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
} catch (IOException e) {
e.printStackTrace();
}
mImageview.setImageBitmap(bitmapImage);
}
}
Uri returnUri;
returnUri = data.getData(); #This is what i want to control

}

@OnClick(R.id.btn_post)
public void onClick(View view) {

#Here is what i ask, How can i use retrunUri on here?
#On Android studio, retrunUri's font color is red, and not works.
Bitmap bitmap = getBitmapFromUri(returnUri);
File imageFile = createFileFromBitmap(bitmap);

RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("name", makeImageFileName() , RequestBody(MediaType.parse("image/png"), imageFile))
.build();
PostApiService.uploadFile(body);
}

# getBitmapFromUri(Uri uri) is for get Bitmap from uri, I omitted opts(options)
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getActivity().getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap resizedBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
parcelFileDescriptor.close();
return resizedBitmap;
}

#createFileFromBitmap(Bitmap bitmap) is for making File from Bitmap
private File createFileFromBitmap(Bitmap bitmap) throws IOException {
File newFile = new File(getActivity().getFilesDir(), makeImageFileName());
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
return newFile;
}

#ImageFileName() is for setting filename
private String makeImageFileName() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
Date date = new Date();
String strDate = simpleDateFormat.format(date);
return strDate + ".png";
}


What I want to know is how to use
returnUri
which is from
onActivityResult
, in
public void onClick(View view)
?

I want to use
returnUri
in
public void onClick(View view)
.

I used an example which is from youtube, but it's too hard for me to understand perfectly,
So please help me.

Answer Source

modify the onActiivtyResult then handle the Exception in OnClick

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == Activity.RESULT_OK) {
        if(requestCode == 1000){
            Uri returnUri = data.getData();
            Bitmap bitmapImage = null;
            try {
                bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mImageview.setImageBitmap(bitmapImage);
        }
    }

    returnUri = data.getData(); //This is what i want to control

}
Uri returnUri;