如何在位置保存图像并在Android App中将其检索为imageView? [英] How to get save an image at location and retrieve it for imageView in Android App?

查看:82
本文介绍了如何在位置保存图像并在Android App中将其检索为imageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个在Android中使用的简单相机应用,以在imageView中显示它.我可以获得非常模糊的缩略图.因此,我为此创建了一个文件,然后它给了我文件的位置.当我检查它时,它的大小为0 kb,这是众所周知的. 我的任务是捕获图像并保存在mCurrentPhotoPath上,我应该能够在startActivityForResult()

I am building a simple camera app in Android to show it in a imageView .I can get thumbnail which is very blurr .So I have a made file for that and then It gives me the location of the file. When I check it ,Its is of 0 kb which is well understood . My Task is to capture image and save at mCurrentPhotoPath and I should be able to display it in my imageView in startActivityForResult()

但是应用程序崩溃在校准之前一遍又一遍. 我知道这行有问题

But App Crashes Before Caling Before that again and again. I know something is buggy in this line

 Uri photoURI = FileProvider.getUriForFile(MainActivity.this,"com.infolabs.photu", photoFile);

.我已经将Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider",photoFile);更改为com.infolabs.photu,但是我无法弄明白其中有什么问题.
请查看我的代码,我具有位置URL的位置,但是由于图像为0KB,因此无法显示或工作

.I have changed Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider",photoFile); to com.infolabs.photu .But I can't get whats wrong in it .
Please have a look at my code and I have location URL location but It won't show or work as the image is 0KB

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.captureimage);
    imageView = (ImageView) findViewById(R.id.imageView);


    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


            if (takePictureIntent.resolveActivity(getPackageManager()) != null)
            {
                // Create the File where the photo should go
                File photoFile = null;
                try
                {
                    photoFile = createImageFile();
                    Toast.makeText(MainActivity.this,mCurrentPhotoPath,Toast.LENGTH_LONG).show();
                }
                catch (IOException ex)
                {
                    Toast.makeText(MainActivity.this,"the file is not created ",Toast.LENGTH_SHORT).show();
                }

                if (photoFile != null)
                {
                    //this Uri does not working properly
                    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,"com.infolabs.photu", photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }
    });
}

错误日志:- 致命异常:主要 流程:infolabs.photu,PID:21415 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法"android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)" 在android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583) 在android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) 在android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399) 在infolabs.photu.MainActivity $ 1.onClick(MainActivity.java:71) 在android.view.View.performClick(View.java:5269) 在android.view.View $ PerformClick.run(View.java:21556) 在android.os.Handler.handleCallback(Handler.java:815) 在android.os.Handler.dispatchMessage(Handler.java:104) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5776) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 04-17 16:56:15.085 21415-21425/infolabs.photu I/System:FinalizerDaemon:完成对象= 65

Error Log:- FATAL EXCEPTION: main Process: infolabs.photu, PID: 21415 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399) at infolabs.photu.MainActivity$1.onClick(MainActivity.java:71) at android.view.View.performClick(View.java:5269) at android.view.View$PerformClick.run(View.java:21556) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 04-17 16:56:15.085 21415-21425/infolabs.photu I/System: FinalizerDaemon: finalize objects = 65

推荐答案

从Camera捕获图像并将其保存到sdcard:

Capture the image from Camera and save it to sdcard:

cameraBtn.setOnClickListener(new View.OnClickListener()
 {
                            @Override
                            public void onClick(View v) 
{

         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    try {

                        File f = createImageFile();//createImageFile() is added.
                        if (f != null) {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                           startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
                        }
                    } catch (IOException IOE) {
                        IOE.printStackTrace();
                    }

                            }
                        });


         private File createImageFile() throws IOException {

                String imageFileName = "image";
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                imageFileName = imageFileName + timeStamp.toString();
                File albumF = getStorageDir();
                File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
                return imageF;

            }

         private File getStorageDir() {
                File storageDir = null;
                storageDir = new File(Environment.getExternalStorageDirectory(), "/MyApp" );
                if (storageDir != null) {
                    if (!storageDir.mkdirs()) {
                        if (!storageDir.exists()) {
                            Log.d("CameraSample", "failed to create directory");
                            return null;
                        }
                    }
                }
                return storageDir;
            }



        private File getImageFile() {
            String Path = Environment.getExternalStorageDirectory() + "/MyApp";
            File f = new File(Path);
            File imageFiles[] = f.listFiles();

            if (imageFiles == null || imageFiles.length == 0) {
                return null;
            }

            File lastModifiedFile = imageFiles[0];
            for (int i = 1; i < imageFiles.length; i++) {
                if (lastModifiedFile.lastModified() < imageFiles[i].lastModified()) {
                    lastModifiedFile = imageFiles[i];
                }
            }
            return lastModifiedFile;
        }

//从活动中覆盖方法,读取保存的图像文件并在ImageView中显示:

// overwrite the method from activity, read the save image file and show in the ImageView:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                File imageFile = getImageFile();

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
                    mImageView.setImageBitmap(bitmap);

}}}

在清单文件中添加权限:

Add permission in the menifest file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.CAMERA" />

这篇关于如何在位置保存图像并在Android App中将其检索为imageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆