简体   繁体   English

将ImageView(从相机捕获)中的图像保存到内部存储器的自定义文件夹中

[英]Save the image from an ImageView(captured from camera) in my custom defined folder in the INTERNAL MEMORY

I have been referring various posts and sites and I have been trying this since 3 days and I cannot figure this out! 我一直在推荐各种帖子和网站,并且自3天以来一直在尝试此操作,我无法弄清楚!

My Activity has 2 buttons! 我的活动有2个按钮! First one is Capture and second one is Save. 第一个是Capture,第二个是Save。 On pressing the capture button, I need to launch the camera and then when we click the picture and click on OK., the image is set to the ImageView in the activity. 在按下捕获按钮时,我需要启动相机,然后在单击图片并单击确定时,将图像设置为活动中的ImageView。 Now, when I click on the save button., I need to save the image in the internal memory in my own folder which must appear in the File explorer . 现在,当我单击“保存”按钮时,需要将图像保存在内存中我自己的文件夹中,该文件夹必须出现在“文件资源管理器”中 Say MyCustomFolder>Pictures>MyCapture.jpg 说出MyCustomFolder>图片> MyCapture.jpg

I have tried several examples but what I was able to do in one case was to store images in default directories such as Downloads or DCIM or PICTURES. 我尝试了几个示例,但是在一种情况下,我能够将图像存储在默认目录中,例如Downloads或DCIM或PICTURES。

In other case, I was able to store the picture in Android>data>com.example.shravan.camera>files>Pictures>myImage.jpg 在其他情况下,我能够将图片存储在Android> data> com.example.shravan.camera> files> Pictures> myImage.jpg中

In the case I am posting here, I am able to save the image in /data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile 如果我要在此处发布,则可以将图像保存在/data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile中

Here I have one more method saveFile which is being commented as I am getting FileNotFound exception as always! 在这里,我还有另一种方法saveFile,因为我一如既往地收到FileNotFound异常

However, I am not able to save in my own customfolder. 但是,我无法保存在自己的customfolder中。 Please check the code and help me and let me know if I am missing something! 请检查代码并为我提供帮助,如果我缺少某些东西,请通知我!

Code: 码:

MainActivity.java MainActivity.java

package com.example.shravan.asbdbsadbsabcxscsa;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;    
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    Uri imageUri;
    private static final String TAG = "abc";
    static final int REQUEST_IMAGE_CAPTURE =1 ;
    ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.myIV);
    }

    public void Capture(View v){
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,REQUEST_IMAGE_CAPTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
        {
            print("in onAC");
            imageUri=data.getData();
            iv.setImageURI(imageUri);
        }
    }

    public void savee(View v){
        Context context =this;
        BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        File mydir = context.getDir("mydir", Context.MODE_PRIVATE); 
        File fileWithinMyDir = new File(mydir, "myfile"); 
        try {
            FileOutputStream fos = new FileOutputStream(fileWithinMyDir);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            print("path is"+fileWithinMyDir.getAbsolutePath());
            fos.flush();
            fos.close();
        }
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void saveFile(View v){
        Context context =this;
        BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        File mydir = context.getFilesDir();
        String filename = "Arunachala/Shravan/Images/MyImage.jpg";
        File file = new File(mydir, filename);       
        file.mkdirs();     
        print("path is"+file.getAbsolutePath());
        try {
            FileOutputStream fos = new FileOutputStream(file);
            print("path is"+file.getAbsolutePath());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void print(String s){
        Log.d(TAG, s);
    }

} }

This is my content_main.xml file: content_main.xml 这是我的content_main.xml文件: content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shravan.asbdbsadbsabcxscsa.MainActivity"
tools:showIn="@layout/activity_main">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myIV"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="175dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Capture"
    android:id="@+id/myB"
    android:onClick="Capture"
    android:layout_alignParentBottom="true"
    android:layout_toStartOf="@+id/myIV" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="save"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_toEndOf="@+id/myB"
    android:onClick="savee" />
</RelativeLayout>

This is my AndroidManifest.xml file: AndroidManifest.xml 这是我的AndroidManifest.xml文件: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shravan.asbdbsadbsabcxscsa">

<uses-feature android:name="android.hardware.Camera"       android:required="true"></uses-feature>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

My latest logcat: 我最新的日志:

08-09 14:28:38.889 12727-12727/com.example.shravan.asbdbsadbsabcxscsa D/abc: path is/data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile 08-09 14:28:38.889 12727-12727 / com.example.shravan.asbdbsadbsabcxscsa D / abc:路径为/data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile

Please help! 请帮忙!

Got it working: 得到它的工作:

Code for saving: 保存代码:

    FileOutputStream outStream = null;
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/camtest");
    dir.mkdirs();
    String fileName = String.format("%d.jpg", System.currentTimeMillis());
    File outFile = new File(dir, fileName);
    outStream = new FileOutputStream(outFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    outStream.flush();
    outStream.close();
    refreshGallery(outFile);

Permissions: 权限:

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

Finally: 最后:

Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission! 转到设备设置>设备>应用程序>应用程序管理器>“您的应用程序”>权限>启用存储权限!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将捕获的图像从相机保存到自定义文件夹反应本地 - Save captured image from camera to custom folder react native 从相机捕获的图像不在imageview android中显示 - Image captured from camera not displaying in imageview android 如何在 android 中保存从相机拍摄的图像 - How to save captured image from camera in android 无法使用android中的自定义相机将捕获的图像保存在文件夹中 - Can't save captured image in folder using custom camera in android 保存在 ImageView 中时,从自定义相机拍摄图像会被拉伸 - Taking image from custom camera is stretched when save in ImageView 如何将相机中的图像保存在内存中? - How I can save an image from the camera in the internal memory? 如何调整从相机捕获的图像大小并显示在imageview上 - How to resize image that captured from camera and display on imageview Android将自定义相机中的图像保存到SD卡上的自定义文件夹中 - Android Save Image from Custom Camera to Custom Folder on SD Card 如何将相机拍摄的图像保存在手机内存中以检查文件大小? - How to save the captured image from camera in the phone memory to check the file size? 从相机文件夹将图像设置为imageview - Set image into imageview from camera folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM