2016-11-25 113 views
-1

我在我的Activity中有兩個按鈕和一個ImageView。當我點擊一個按鈕時,它會重定向到圖庫,我可以選擇一個圖像。所選圖像將顯示在我的ImageView中。 現在的問題是我想要保存按鈕來保存圖像顯示在一個新的目錄,就像它在像instagram一樣的應用程序中。從圖庫中選擇圖像,在imageview中顯示並保存在新目錄中顯示的圖像

我的佈局如下:

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="imgClick" 
     android:id="@+id/imgButton" 
     android:text="Select Image" 
     android:layout_marginTop="20dp" /> 

    <ImageView 
     android:layout_height="100dp" 
     android:layout_width="100dp" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/diddyImageView" 
     android:layout_alignParentTop="true" 
     android:scaleType="fitXY" 
     android:maxWidth="100dp" 
     android:maxHeight="100dp"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="addMovieClick" 
     android:id="@+id/addMovieButton" 
     android:text="Submit Movie" 
     android:background="@color/colorPrimary" 
     android:layout_marginTop="20dp" /> 

我的活動如下

public void imgClick(View v){ 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECTED_PICTURE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == SELECTED_PICTURE && resultCode == RESULT_OK) { 

      uri = data.getData(); 
      diddyImageView.setImageURI(uri); 

     } 
} 

圖像給出正確顯示,我想將圖像保存在一個新的文件夾,我不知道如何去幫助it..Any會appreciated..Thanks

+0

'我想保存圖像在一個新的文件夾'如何使用簡單的文件副本? –

回答

0

第1步:使用ContentResolveropenInputStream()要打開UriInputStream到圖像

第2步:打開所需的文件FileOutputStream爲您的副本

第3步:使用標準的Java I/O從InputStream字節複製到FileOutputStream

第4步:將所有的這工作到後臺線程(並考慮使用圖像加載庫來取代你的setImageURI()調用),以避免捆綁主要應用程序線程做所有這些工作

相關問題