文件在文件资源管理器中无法正确显示

时间:2019-09-05 00:56:51

标签: android fileoutputstream image-editing

我有一个可以拍照的应用程序,然后允许用户在新活动中使用它进行绘画。在应用程序中,一切都按照应有的方式运行。标记后的图像将返回并可以加载到报告,其他视图等中。

但是,当我在移动设备上使用文件资源管理器导航到图片位置时,显示的缩略图是没有标记的原始图片,即使刷新视图也是如此。如果我尝试从资源管理器中加载图片,有时必须多次加载它,或使用其他图片查看器程序查看该图片的最新版本。

由于该应用程序正常运行,实际上只不过是一个小小的烦恼,但是令人沮丧的是,我不明白为什么会出现差异。我怀疑这与saveBitmap方法中的“ fileoutputstream”有关。谁能解释发生了什么事?

完全公开,此代码不是原始代码。我从多个线程中发现的零零碎碎地把它完全科学了起来。我知道那使我成为一个可怕的人。你不必告诉我。

{
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class EditPictureActivity extends AppCompatActivity {
    Button btnSaveImage;
    ImageView imageResult;
    Uri source;
    Bitmap bitmapMaster;
    Canvas canvasMaster;

    int prvX, prvY;

    Paint paintDraw;
    String filename = "apple";
    String name;
    String file;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_picture);

        btnSaveImage = (Button)findViewById(R.id.saveimage);
        imageResult = (ImageView)findViewById(R.id.result);

        paintDraw = new Paint();
        paintDraw.setStyle(Paint.Style.FILL);
        paintDraw.setColor(Color.YELLOW);
        paintDraw.setStrokeWidth(10);

        if(getIntent().hasExtra("filename") ){
            filename = getIntent().getStringExtra("filename");
            name = getIntent().getStringExtra("name");
            file = getIntent().getStringExtra("file");
        }

        source = (Uri) Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Pictures/" + filename + ".jpg"));
        Bitmap tempBitmap;
        try {
            //tempBitmap is Immutable bitmap,
            //cannot be passed to Canvas constructor
            tempBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(source));

            Bitmap.Config config;
            if(tempBitmap.getConfig() != null){
                config = tempBitmap.getConfig();
            }else{
                config = Bitmap.Config.ARGB_8888;
            }

            //bitmapMaster is Mutable bitmap
            bitmapMaster = Bitmap.createBitmap(tempBitmap.getWidth(),tempBitmap.getHeight(),config);

            canvasMaster = new Canvas(bitmapMaster);
            canvasMaster.drawBitmap(tempBitmap, 0, 0, null);

            imageResult.setImageBitmap(bitmapMaster);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        imageResult.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int action = event.getAction();
                int x = (int) event.getX();
                int y = (int) event.getY();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        prvX = x;
                        prvY = y;
                        drawOnProjectedBitMap((ImageView) v, bitmapMaster, prvX, prvY, x, y);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        drawOnProjectedBitMap((ImageView) v, bitmapMaster, prvX, prvY, x, y);
                        prvX = x;
                        prvY = y;
                        break;
                    case MotionEvent.ACTION_UP:
                        drawOnProjectedBitMap((ImageView) v, bitmapMaster, prvX, prvY, x, y);
                        break;
                }
                /*
                 * Return 'true' to indicate that the event have been consumed.
                 * If auto-generated 'false', your code can detect ACTION_DOWN only,
                 * cannot detect ACTION_MOVE and ACTION_UP.
                 */
                return true;
            }
        });
    }

    /*
    Project position on ImageView to position on Bitmap draw on it
     */

    private void drawOnProjectedBitMap(ImageView iv, Bitmap bm,
                                       float x0, float y0, float x, float y){
        if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){
            //outside ImageView
            return;
        }else{

            float ratioWidth = (float)bm.getWidth()/(float)iv.getWidth();
            float ratioHeight = (float)bm.getHeight()/(float)iv.getHeight();

            canvasMaster.drawLine(
                    x0 * ratioWidth,
                    y0 * ratioHeight,
                    x * ratioWidth,
                    y * ratioHeight,
                    paintDraw);
            imageResult.invalidate();
        }

        btnSaveImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bitmapMaster != null){
                    saveBitmap(bitmapMaster);
                }


                Intent intent = new Intent(EditPictureActivity.this, AddRecordActivity.class);
                intent.putExtra("filename", filename);
                intent.putExtra("name", name);
                intent.putExtra("file", file);
                startActivity(intent);
            }
        });

    }


    private void saveBitmap(Bitmap bm){
        File file = Environment.getExternalStorageDirectory();
        File newFile = new File(file, "/Pictures/" + filename + ".jpg");

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(newFile);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            Toast.makeText(EditPictureActivity.this, "Picture has been Updated.", Toast.LENGTH_LONG).show();
        }

        catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(EditPictureActivity.this,
                    "Something wrong: " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }

        catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(EditPictureActivity.this,
                    "Something wrong: " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }
}
}

0 个答案:

没有答案