2013-10-02 50 views
0

让我首先说我是编程新手。我试图创建一个私人画廊,我已经搜索了网页,并得到了点点滴滴一起创建这个应用程序将图片从用户图库复制到SD卡上的文件夹

我有基本库下来,它显示我在我的私人文件夹在网格中的图像和我可以选择一个图像放大缩小它以放大和缩小所有这些好东西。

我现在想要做的是...当用户启动他们的正常画廊并选择一个图像(或视频),然后选择共享选项时,他们选择我的应用程序。我希望它将文件复制到我的私人位置“/DCIM/privgal/.nomedia/”,然后从其普通图库中删除该文件。

我现在正在使用HTC ONE进行所有测试,当我从“共享”菜单中选择我的应用程序时,图库崩溃并想要将报告发送给HTC。我在LogCat中看不到任何错误,就好像它从未实际调用我的应用程序,所以我看不到有什么问题。

我知道下面的代码是一团糟,我知道它不是按照原样运行的,但正如我之前说的,我是这个新手,并将这些零碎收集在一起,并希望得到它与错误的工作登录猫会给我。不幸的是,它不报告任何错误,所以我被卡住了。

有人可以看看这个,并指出我在一个工作示例的方向或...我真的很讨厌说它,修复我的代码?

任何帮助表示赞赏!

史蒂夫

Working Code Below posted by Me. (see Below 10/27/2013) 

回答

1

活动只有私有方法,没有onCreate覆盖方法,所以没有方法被调用。实际上这个活动什么都不做,没有看法,因此应用程序根本就没有工作。

您需要覆盖onCreate方法,使用getInvent作为,明确,获取意图,然后getData为内容等。

+0

我改变了上面的代码(见上)以包含onCreate的,但它仍然无法正常工作。 – Sobo

2

这里是工作的代码以使用“发送到/ Share菜单”,从画廊到的图像复制到一个预定义的隐藏文件夹的存储和从用户删除文件库

SendToActivity.java

package com.company.privategallery; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.Gravity; 
import android.view.KeyEvent; 
import android.widget.Toast; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class SendToActivity extends Activity { 

     private static final int SELECT_PICTURE = 0; 
     //Generating Random Number to use as a unique file name 
     static int random = (int)Math.ceil(Math.random()*100000000); 
     private static String fname = Integer.toString(random); 

     private static String selectedImagePath; 
     //Getting the external Path to the Storage 
     private static String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     //Setting a directory that is already created on the Storage to copy the file to. 
     private static String targetPath = ExternalStorageDirectoryPath + "/DCIM/privgal/.nomedia/"; 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      //get the received intent 
      Intent receivedIntent = getIntent(); 

      //get the action 
      String receivedType = receivedIntent.getType(); 

      //make sure it's an action and type we can handle 
       if(receivedType.startsWith("image/")){ 
        //get the uri of the received image 
        Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM); 
        selectedImagePath = getPath(receivedUri); 
        System.out.println("Image Path : " + selectedImagePath); 
        //check we have a uri 
        if (receivedUri != null) { 
         //Copy the picture 
          try { 
           copyFile(selectedImagePath, targetPath + fname + ".jpg"); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          startGallery(); 
          deleteFile(); 
          onDestroy(); 
        } 
       } 
     } 

     public void copyFile(String selectedImagePath, String string) throws IOException { 
      InputStream in = new FileInputStream(selectedImagePath); 
      OutputStream out = new FileOutputStream(string); 

      // Transfer bytes from in to out 
      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = in.read(buf)) > 0) { 
       out.write(buf, 0, len); 
      } 
      in.close(); 
      out.close(); 
      Toast customToast = new Toast(getBaseContext()); 
      customToast = Toast.makeText(getBaseContext(), "Image Transferred", Toast.LENGTH_LONG); 
      customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0); 
      customToast.show(); 
      } 

     private void startGallery() { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.addCategory(Intent.CATEGORY_OPENABLE); 
      startActivityForResult(intent, SELECT_PICTURE); 
     } 

     // Delete the file that was copied over 
     private void deleteFile() { 
      File fileToDelete = new File(selectedImagePath); 
      boolean fileDeleted = fileToDelete.delete(); 

      // request scan  
      Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
      scanIntent.setData(Uri.fromFile(fileToDelete)); 
      sendBroadcast(scanIntent); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 
      finish(); 
     } 

     public String getPath(Uri uri) { 
      String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      android.os.Process.killProcess(android.os.Process.myPid()); 
      finish(); 

     } 

    } 

新增活动和权限在马纳费斯特

权限:

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

活动:

<activity android:name="com.company.privategallery.SendToActivity" 
     android:exported="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:mimeType="image/*" /> 
      </intent-filter> 
    </activity> 
+0

** MISLEADING和ERRONEOUS ** - 没有内部**存储清单许可! –

相关问题