3

I've created a method in my app that saves my layout as an image and presents it in the gallery. I'm trying to open it but I'm having a hard time with all the tutorials and different approaches...

This is my code:

SimpleDateFormat mTimeFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date mTime = new Date();
        String mStringTime = mTimeFormat.format(mTime);

        String fileFolder = "/orders";
        String fileName = "/order-"+mStringTime;
        File filePath = new File("/sdcard"+fileFolder+fileName+".png");
        Uri fileUri = Uri.fromFile(filePath);

        File createFolder = new File(Environment.getExternalStorageDirectory()+fileFolder);
        if (!createFolder.exists()){ //checks I don't create the same folder twice
            createFolder.mkdir();
        }

        View orderView = findViewById(R.id.tableOrder); //this is what i shall save
        orderView.setDrawingCacheEnabled(true);
        Bitmap bitmap = orderView.getDrawingCache();      

        try {
            FileOutputStream outputImage = new FileOutputStream(filePath);
            bitmap.compress(CompressFormat.PNG, 10, outputImage);
            outputImage.close();
            orderView.invalidate();     

            Intent intentScanner = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intentScanner.setData(fileUri);
            sendBroadcast(intentScanner);

        } 
        catch (Exception e) 
        { e.printStackTrace();
        } finally{
            orderView.setDrawingCacheEnabled(false);  

            Intent intentView = new Intent(Intent.ACTION_VIEW);
            intentView.setType("image/*");
            intentView.setData(fileUri);
            startActivity(intentView);
        }

when running the app crashes, but the image is being saved

android.content.ActivityNotFoundException: No Activity found to handle Intent

premissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

What should I do?

EladB
  • 326
  • 5
  • 15

2 Answers2

7
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, <unique_code>);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == <unique_code>) {
        imageView.setImageBitmap(getPicture(data.getData()));
    }
}

public static Bitmap getPicture(Uri selectedImage) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();
    return BitmapFactory.decodeFile(picturePath);
}
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
mbmc
  • 5,024
  • 5
  • 25
  • 53
  • I don't really get it. how do I get the imageView you used on onActivityResult? I don't want to display my the image, I want to open it as a part of the gallery (intent to the gallery app) – EladB Aug 06 '14 at 19:53
  • Sorry, my bad... I read your question too fast. The sample I provided is for retrieving the image selected from the gallery. – mbmc Aug 06 '14 at 20:13
1

To open Gallery App from your code, call this

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
     "content://media/internal/images/media")); 
     startActivity(intent);

Edit:

To open the recently saved image

public void openInGallery(String imageId) {
  Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(imageId).build();
  Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  startActivity(intent);
}

All you have to do is append the image id to the end of the path for the EXTERNAL_CONTENT_URI. Then launch an Intent with the View action, and the Uri.

The image id comes from querying the content resolver.

vijay_t
  • 776
  • 6
  • 14
  • How does it help me? I want to open the image I just created - no the entire gallery... – EladB Aug 06 '14 at 19:45